let ajaxTimes = 0;

module.exports = (param) => {
	var url = param.url;
	var method = param.method;
	var header = param.header || {};
	var data = param.data || {};
	var isLoading = param.isLoading||false
	// 请求方式: GET POST 
    const userToken = uni.getStorageSync('hgylsstoken')
	if (method) {
		method = method.toUpperCase(); // 小写转成大写
		if (method == "POST") {
			header["content-type"] = "application/x-www-form-urlencoded";
        }
	}
    if(userToken){
        header["token"] = userToken;
    }
	ajaxTimes++
	if(!isLoading){
		uni.showLoading({
			title: "加载中...",
			mask: true
		})
	}
	
	setTimeout(()=>{
		if(ajaxTimes>0){
			uni.hideLoading();
		}
	},10000)
	return new Promise((resolve, reject) => {
		// 发起网络请求
		uni.request({
			url: url,
			method: method || "GET",
			header: header,
			data: data,
			success: res => {
				const {
					data
				} = res
				if (res.statusCode && res.statusCode == 301 || data && data.code && data.code == 999) {
					uni.showModal({
						title: '提示',
						content: '未登录或登录超时，请重新登录！',
						success(res) {
							showNum = 0
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/nongqiatuan/login/login'
								})
							}
							if (res.cancel) {
								uni.switchTab({
									url: '/pages/nongqiatuan/index/index'
								})
							}
						}
					})
					return
				}
				resolve(data)
			},
			fail: (e) => {
				reject(e)
			},
			complete: (e) => {
				ajaxTimes--;
				if (ajaxTimes === 0) {
					uni.hideLoading();
				}
			}
		})
	})
}
